<script setup lang='ts' name="policy-add">
import type { PlusColumn } from 'plus-pro-components'

import { resetPwd, resetPwdForIam } from '@fl/api/iam-api'
import { SubmitBtn } from '@fl/components'
import { cloneDeep } from 'lodash-es'

const visible = defineModel<boolean>('visible', { required: true })
const userId = defineModel<string>('userid', { required: true })
const loginName = defineModel<string>('loginname', { required: true })

const props = withDefaults(defineProps<{
    catTypeIdStr: string
}>(), {
    catTypeIdStr: '',
})

const { catTypeIdStr } = toRefs(props)
const catTypeIdArr = catTypeIdStr.value.split(',')
const hasIamBusiness = catTypeIdArr.some(tId => ['9', '10'].includes(tId)) // 9适老化、10电梯加装

const form = ref()
const title = ref('重置密码')

const initialState = {
    loginName: '',
    password: '',
    userId: '',
}
const state = ref(cloneDeep(initialState))

watchEffect(() => {
    if (loginName.value) {
        state.value.loginName = loginName.value
        state.value.userId = userId.value
    }
})

// 提交
async function submit() {
    const valid = await form.value.formInstance.validate()
    if (valid) {
        try {
            const params = {
                loginName: state.value.loginName,
                password: state.value.password,
                userId: Number(state.value.userId),
            }

            const res1 = await resetPwd(params)
            let isSuccess = res1.code === 200

            if (hasIamBusiness) {
                const res2 = await resetPwdForIam({ userName: state.value.loginName })
                isSuccess = isSuccess && res2.code === 200
            }

            if (isSuccess) {
                ElMessage.success('操作成功')
                cancel()
            } else {
                ElMessage.error('操作失败')
            }
        } catch (err) {
            console.error(err)
        }
    }
}

function cancel() {
    visible.value = false
}

function reset() {
    userId.value = ''
    form.value.formInstance.resetFields()
}

const columns: PlusColumn[] = [
    {
        fieldProps: {
            disabled: true,
        },
        label: '登录名称',
        prop: 'loginName',
        valueType: 'input',
    },
    {
        label: '输入密码',
        prop: 'password',
    },
]

const rules = {
    password: [
        {
            message: '新密码不能为空',
            required: true,
        },
    ],
}
</script>

<template>
    <PlusDialog v-model="visible"
                :title="title"
                width="500"
                close-on-press-escape
                :has-footer="false"
                @cancel="cancel"
                @closed="reset"
    >
        <div class="mt-20">
            <PlusForm ref="form"
                      v-model="state"
                      :columns="columns"
                      :label-width="120"
                      :rules="rules"
                      :has-footer="false"
                      :row-props="{ gutter: 20 }"
            />
        </div>

        <div class="mx-auto mt20 flex justify-center">
            <el-button @click="cancel">
                取消
            </el-button>

            <SubmitBtn type="primary"
                       @click="submit"
            >
                提交
            </SubmitBtn>
        </div>
    </PlusDialog>
</template>
